package voa;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import util.FileProcessor;

public class ParagraphFileProcessor implements FileProcessor {

	@Override
	public void process(File file) {
				
		String inFileName = file.getAbsolutePath();
		String outFileName = getOutputPath() + "/" + getOutputName(file);

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(new File(inFileName)), "unicode"));

			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(new File(outFileName)), "utf-8"));

			String lineStr = "";
			String sentence = "";
			int line = 0;
		
			
			while (null != (lineStr = reader.readLine())) {
				
				 if(line > 3 ){
					
					if (sentence.equals("")){
						
						lineStr.trim();
						
						sentence += lineStr.substring(0, 10) + "  "
						+ lineStr.substring(10, lineStr.length());
						
					} else {
						
						lineStr = lineStr.substring(10, lineStr.length()).trim();
						
						sentence += " " + lineStr;
					}
					
					if(sentence.endsWith(".") || sentence.endsWith("\"") ){
						
						writer.write(sentence + "\n");
						sentence = "";
					}
				} else {
					
					writer.write(lineStr + "\n");
				}
					
				line++;
			}
			
		

			reader.close();
			writer.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public String getOutputPath(){
		
		return "/home/gust/tmp/lrcout";
	}
	
	public String getOutputName(File file){
		
		return file.getName();
		
	}

}
